<template>
  <div class="box">
    <div class="scroll-view">
      <projectMsg :projectId="project_id" :hrefProject="true"></projectMsg>
      <div class="user-title-box">基础信息</div>
      <el-form :model="dkbjRuleForm" :rules="dkbjRules" ref="dkbjRuleForm" label-position="top">
        <el-form-item label="报价金额" prop="old_amount">
          <el-input class="input-box" disabled v-model="dkbjRuleForm.old_amount" clearable v-input-limit="2"></el-input>
        </el-form-item>
        <el-form-item label="预计签约金额" prop="new_amount">
          <el-input class="input-box" v-model="dkbjRuleForm.new_amount" clearable v-input-limit="2"></el-input>
        </el-form-item>
        <el-form-item label="优惠金额" prop="discount_amount" v-if="dkbjRuleForm.discount_amount">
          <el-input disabled class="input-box" v-model="dkbjRuleForm.discount_amount" clearable
            v-input-limit="2"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input :rows="4" class="input-box" v-model="dkbjRuleForm.remark" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button v-if="showTip" class="tips" type="text"
          >已超出优惠权限，需客户总监审批</el-button
        > -->
        <div class="flex ac">
          <span>
            <el-button type="primary" @click="dkbjSubmit('dkbjRuleForm')" :disabled="diaLoading"
              :loading="diaLoading">提交</el-button>
            <el-button @click="dkbjCancel('dkbjRuleForm')">关闭</el-button>
          </span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import projectApi from "@/api/projectApi";
import { getUserInfo } from "@/utils/auth";
import projectMsg from "@/components/ProjectMsg";
import * as math from "mathjs";

export default {
  name: "dkbjCom",
  components: {
    projectMsg,
  },
  data() {
    return {
      dkbjRuleForm: {
        remark: "",
        old_amount: "",
        new_amount: "",
        discount_amount: "",
      },
      dkbjRules: {
        old_amount: [
          { required: true, message: "请输入原报价金额", trigger: "change" },
        ],
        new_amount: [
          { required: true, message: "请输入预计签约金额", trigger: "change" },
        ],
        discount_amount: [
          { required: true, message: "请输入优惠金额", trigger: "change" },
        ],
      },
      diaLoading: false,
      showTip: false,
      zjData: "",
      project_id: "",
    };
  },
  watch: {},
  computed: {},
  mounted() {
    this.project_id = this.$route.query.id;
    this.getProjectLog();
  },
  methods: {
    inputChange() {
      let { discount_ratio } = getUserInfo();
      let dkbjRuleForm = this.dkbjRuleForm;
      let discount_amount = "";
      if (dkbjRuleForm.old_amount && dkbjRuleForm.new_amount) {
        discount_amount = Number(
          math.format(
            math.subtract(
              math.bignumber(dkbjRuleForm.old_amount),
              math.bignumber(dkbjRuleForm.new_amount)
            )
          )
        );
        dkbjRuleForm.discount_amount = discount_amount;
        if (discount_amount) {
          let bl = Number(
            math.format(
              math.divide(
                math.bignumber(discount_amount),
                math.bignumber(dkbjRuleForm.old_amount)
              )
            )
          );
          bl = bl * 100;
          this.showTip =
            bl > Number(discount_ratio) && bl != Number(discount_ratio);
        } else {
          this.showTip = false;
        }
      } else {
        this.showTip = false;
        dkbjRuleForm.discount_amount = "";
      }
      this.dkbjRuleForm = dkbjRuleForm;
    },
    // 对客报价
    dkbjCancel() {
      this.$goBackTab(this);
    },
    dkbjSubmit(formName) {
      let dkbjRuleForm = this.dkbjRuleForm;
      console.log(dkbjRuleForm, "对客报价参数");
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.diaLoading = true;
          projectApi
            .toCustomerQuotation({
              project_id: this.project_id,
              remark: dkbjRuleForm.remark,
              old_amount: dkbjRuleForm.old_amount,
              new_amount: dkbjRuleForm.new_amount,
              discount_amount: dkbjRuleForm.discount_amount,
            })
            .then((res) => {
              console.log(res, "对客报价");
              this.diaLoading = false;
              if (res.code == 200) {
                if (res.data.is_review == 2) {
                  this.$confirm('超出优惠权限是否去提交优惠审批?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                  }).then(() => {
                    this.$goBackTab(this, () => {
                      this.$router.push({
                        path: '/addDiscount'
                      })
                    });
                  }).catch(() => {
                    this.dkbjCancel();
                  });
                } else {
                  this.dkbjCancel();
                }
              }
              this.$message({
                type: res.code == 200 ? "success" : "warning",
                message: res.code == 200 ? "处理成功" : res.message,
              });
            })
            .catch((err) => {
              this.diaLoading = false;
              this.$message({
                type: "warning",
                message: err.message,
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 获取项目日志
    getProjectLog() {
      this.logList = [];
      this.logLoaidng = true;
      projectApi
        .getProjectLogList({
          project_id: this.$route.query.id,
          log_type: 0,
        })
        .then((res) => {
          console.log(res, "项目日志");
          this.logLoaidng = false;
          if (res.code == 200) {
            res.data.forEach((item) => {
              if (item.form_data) {
                item.form_data = JSON.parse(item.form_data);
              }
              // 拿到最新一条造价数据
              if (!this.zjData && item.log_status == 220) {
                this.zjData = item.form_data;
                this.dkbjRuleForm.old_amount = this.zjData.quotation_amount;
              }
            });
          }
        })
        .catch((err) => {
          this.logLoaidng = false;
        });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/projectOparet.scss";
@import "@/styles/addPage.scss";
</style>